package com.example.blogdemo.service;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;
import java.util.Set;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2017-12-17
 **/
@Service
public class JedisService implements InitializingBean {

    private JedisPool pool;


    @Override
    public void afterPropertiesSet() throws Exception{
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(30);
        config.setMaxIdle(10);
        pool = new JedisPool(config,"127.0.0.1",6379);
    }

    public Jedis getJedis(){
        return pool.getResource();
    }

    public void put(String key,String value){
        Jedis jedis = pool.getResource();
        jedis.set(key,value);
        jedis.close();
    }

    public void incr(String key){
        Jedis jedis = pool.getResource();
        jedis.incr(key);
        jedis.close();
    }

    public String get(String key){
        Jedis jedis = pool.getResource();
        String result = jedis.get(key);
        jedis.close();
        return result;

    }

    /**
     * set添加值
     * @param key
     * @param value
     */
    public void sadd(String key,String value){
        Jedis jedis = pool.getResource();
        jedis.sadd(key,value);
        jedis.close();
    }

    /**
     * set删除值
     * @param key
     * @param value
     */
    public void srem(String key,String value){
        Jedis jedis = pool.getResource();
        jedis.srem(key,value);
        jedis.close();
    }

    /**
     * sismember [setname] [value] 判断值是否存在 返回1存在；0不存在
     * @param key
     * @param value
     * @return
     */
    public boolean sismember(String key,String value){
        Jedis jedis = pool.getResource();
        boolean result = jedis.sismember(key,value);
        jedis.close();
        return result;
    }

    /**
     * scard [setname]集合数量
     * @param key
     * @return
     */
    public long scard(String key){
        Jedis jedis = pool.getResource();
        long result = jedis.scard(key);
        jedis.close();
        return result;
    }

    /**
     * lpush [listname] [value1] [value2]....列表左端加入值
     * @param key
     * @param value
     */
    public void lpush(String key,String value){
        Jedis jedis = pool.getResource();
        jedis.lpush(key,value);
        jedis.close();
    }

    public List<String> brpop(int time,String key){
        Jedis jedis = pool.getResource();
        List<String> list = jedis.brpop(time,key);
        jedis.close();
        return list;
    }

    /**
     * zadd [sortname] [score1][value1][score2][value2] 添加元素
     * @param key
     * @param score
     * @param value
     * @return
     */
    public long zadd(String key,double score,String value){
        Jedis jedis = pool.getResource();
        long result = jedis.zadd(key,score,value);
        jedis.close();
        return result;
    }


    /**
     * zincrby [sortname] [score] [keyvalue] 将相应的分数加[score]
     * @param key
     * @param value
     * @return
     */
    public double zincrby(String key,String value){
        Jedis jedis = pool.getResource();
        double result = jedis.zincrby(key,1,value);
        jedis.close();
        return result;
    }

    /**
     * zrevrange [sorename] [start] [stop] [withscores] 查询区间内的值、是否返回分数 从大到小
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<String> zrevrange(String key, int start,int end){
        Jedis jedis = pool.getResource();
        Set<String> set = jedis.zrevrange(key,start,end);
        jedis.close();
        return set;
    }

    /**
     * zcard [sortname] 查询长度
     * @param key
     * @return
     */
    public long zcard(String key){
        Jedis jedis = pool.getResource();
        long result = jedis.zcard(key);
        jedis.close();
        return result;
    }


    /**
     * zscore [sortname]  [value] 显示value的分数
     * @param key
     * @param member
     * @return
     */
    public Double zscore(String key,String member){
        Jedis jedis = pool.getResource();
        Double result = jedis.zscore(key,member);
        jedis.close();
        return result;
    }
}

